package diagramobjects;
import java.awt.Graphics;
import utils.Color;

import program.DatabaseConnection;
import program.PaintObjects;
import utils.Point;

public class Arrow extends DiagramObject  {
	private Integer Id;
	private Point Begin;
	private Point Left;
	private Point Right;
	private Point End;
	private Boolean Filled;
	private Color ArrowColor;
	
/*	public Arrow(Integer id, Point begin, Point left, Point right, Point end,
			Boolean filled, Color AColor) 
	{
		super();
		Id = id;
		Begin = begin;
		Left = left;
		Right = right;
		End = end;
		Filled = filled;
		ArrowColor = AColor;
	}*/
	
	public Arrow()
	{
		Id = 0;
		Begin = new Point(-1,-1);
		Begin = new Point(-1,-1);
		Left = new Point(-1,-1);
		Right = new Point(-1,-1);
		End = new Point(-1,-1);
		Filled = false;
		ArrowColor = new Color(0,0,0);
	}
	
	public Arrow(Point b, Point e, Boolean filled, Color acolor)
	{
		Id = 0;
		Begin = new Point(b.getX(), b.getY());
		//dodawanie left:
		//Double odl = 10.0;
		Double a =0.0;
		Double x = 0.0;
		Double y = 0.0;
		
		Double mianownikAlfa ;
		//LEFT
		//if ((b.getX() != e.getX() ) &&  (b.getY() != e.getY()))
		{
			 mianownikAlfa = new Double(e.getX() - b.getX());
			if (mianownikAlfa == 0)
				mianownikAlfa = 0.000001;
			a = (e.getY() - b.getY())/mianownikAlfa;
			if (a == 0)
				a = 0.0000000001;
			System.out.println("Aif: " +  a.toString());
		}
	
		Double alfa = java.lang.Math.atan(a);
		alfa = alfa - 5*java.lang.Math.PI/4;
		
		if (mianownikAlfa >= 0)
		{
			alfa = alfa + java.lang.Math.PI;
		}
		
		
		x =  10.0;
		y = 0.0;
		Double xPoObrocie = x*java.lang.Math.cos(alfa) - y*java.lang.Math.sin(alfa);
		Double yPoObrocie = x*java.lang.Math.sin(alfa) + y*java.lang.Math.cos(alfa);
		
		Double wynikoweLeftX = xPoObrocie + b.getX();
		Double wynikoweLeftY = yPoObrocie + b.getY();

		System.out.println("Dodawanie Arrow: alfa: " + alfa.toString() 
				+ "  xPoObrocie: " + xPoObrocie.toString()
				+ "  xPoObrocie" + xPoObrocie.toString());
		
		Left = new Point(wynikoweLeftX.intValue(), wynikoweLeftY.intValue());
		
		//RIGHT
		xPoObrocie = x*java.lang.Math.cos(alfa + java.lang.Math.PI/2) - y*java.lang.Math.sin(alfa + java.lang.Math.PI/2);
		yPoObrocie = x*java.lang.Math.sin(alfa + java.lang.Math.PI/2) + y*java.lang.Math.cos(alfa + java.lang.Math.PI/2) ;
		
		 wynikoweLeftX = xPoObrocie + b.getX();
		 wynikoweLeftY = yPoObrocie + b.getY();
		
		Right = new Point(wynikoweLeftX.intValue(), wynikoweLeftY.intValue());
		End = new Point(b.getX(), b.getY());
		
		Filled = true;
		ArrowColor = new Color(255,0,0);
	}
	
	/*public Arrow(Point b, Point e, Boolean filled, Color acolor, Double obrot, Integer odleglosc)
	{
		Id = 0;
		Begin = new Point(b.getX(), b.getY());
		//dodawanie left:
		//Double odl = 10.0;
		Double a =0.0;
		Double x = 0.0;
		Double y = 0.0;
		
		Double mianownikAlfa ;
		//LEFT
		//if ((b.getX() != e.getX() ) &&  (b.getY() != e.getY()))
		{
			 mianownikAlfa = new Double(e.getX() - b.getX());
			if (mianownikAlfa == 0)
				mianownikAlfa = 0.000001;
			a = (e.getY() - b.getY())/mianownikAlfa;
			if (a == 0)
				a = 0.0000000001;
			System.out.println("Aif: " +  a.toString());
		}
	
		Double alfa = java.lang.Math.atan(a);
		alfa = alfa + java.lang.Math.PI - obrot; //wczesniej alfa = alfa + 5*java.lang.Math.PI/4
		
		if (mianownikAlfa >= 0)
		{
			alfa = alfa + java.lang.Math.PI;
		}
		
		
		x =  odleglosc.doubleValue(); // wczesniej odleglosc = 7.0;
		y = 0.0;
		Double xPoObrocie = x*java.lang.Math.cos(alfa) - y*java.lang.Math.sin(alfa);
		Double yPoObrocie = x*java.lang.Math.sin(alfa) + y*java.lang.Math.cos(alfa);
		
		Double wynikoweLeftX = xPoObrocie + b.getX();
		Double wynikoweLeftY = yPoObrocie + b.getY();

		System.out.println("Dodawanie Arrow: alfa: " + alfa.toString() 
				+ "  xPoObrocie: " + xPoObrocie.toString()
				+ "  xPoObrocie" + xPoObrocie.toString());
		
		Left = new Point(wynikoweLeftX.intValue(), wynikoweLeftY.intValue());
		
		//RIGHT
		//xPoObrocie = x*java.lang.Math.cos(alfa + java.lang.Math.PI/2-obrot/2) - y*java.lang.Math.sin(alfa + java.lang.Math.PI/2-obrot/2);
		//yPoObrocie = x*java.lang.Math.sin(alfa + java.lang.Math.PI/2-obrot/2) + y*java.lang.Math.cos(alfa + java.lang.Math.PI/2-obrot/2) ;
		xPoObrocie = x*java.lang.Math.cos(alfa +2*obrot) - y*java.lang.Math.sin(alfa + 2*obrot);
		yPoObrocie = x*java.lang.Math.sin(alfa + 2*obrot) + y*java.lang.Math.cos(alfa + 2*obrot) ;
		
		 wynikoweLeftX = xPoObrocie + b.getX();
		 wynikoweLeftY = yPoObrocie + b.getY();
		
		Right = new Point(wynikoweLeftX.intValue(), wynikoweLeftY.intValue());
		End = new Point(b.getX(), b.getY());
		
		Filled = true;
		ArrowColor = new Color(255,0,0);
	}
	*/
	
	/**
	 * @return the filled
	 */
	public Boolean getFilled()
	{
		return Filled;
	}

	/**
	 * @return the arrrowColor
	 */
	public Color getArrrowColor()
	{
		return ArrowColor;
	}
	
	public java.awt.Color getAWTColor()
	{
		return new java.awt.Color(ArrowColor.getR(), ArrowColor.getG(), ArrowColor.getB());
	}

	/**
	 * @return the begin
	 */
	public Point getBegin()
	{
		return Begin;
	}

	/**
	 * @return the left
	 */
	public Point getLeft()
	{
		return Left;
	}

	/**
	 * @return the right
	 */
	public Point getRight()
	{
		return Right;
	}

	/**
	 * @return the end
	 */
	public Point getEnd()
	{
		return End;
	}

	@Override
	public void Draw(Graphics g, PaintObjects po) 
	{
		// TODO Auto-generated method stub
		po.DrawArrow(g, Begin.getX(), Begin.getY(), Left.getX(), 
				Left.getY(), Right.getX(), Right.getY(), End.getX(), 
				End.getY(), Filled);
	}

	/*@Override
	public Boolean PointCollision(Integer x, Integer y) 
	{
		// TODO Auto-generated method stub
		return super.PointCollision(x, y);
	}*/

	@Override
	public void Print() 
	{
		// TODO Auto-generated method stub
		super.Print();
	}

	@Override
	public String ToDatabase() 
	{
		// TODO Auto-generated method stub
	
		
		String toReturn = /*Id.toString() + "," +*/
		Begin.getX().toString() + "," +
		Begin.getY().toString() + "," +
		Left.getX().toString() + "," +
		Left.getY().toString() + "," +
		Right.getX().toString() + "," +
		Right.getY().toString() + "," +
		End.getX().toString() + "," +
		End.getY().toString() + "," +
		Filled.toString();
		
		return toReturn;
	}
	
	public void AddToDatabase()
	{
		DatabaseConnection.AddArrow(ToDatabase());
		ArrowColor.AddToDatabase();
	}

	@Override
	public void MoveByVector(Integer vx, Integer vy)
	{
		Begin.MoveByVector(vx, vy);
		Left.MoveByVector(vx, vy);
		Right.MoveByVector(vx, vy);
		End.MoveByVector(vx, vy);
	}
	
	
	
	

}
